{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# MNIST_Tensorboard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting /home/lilin/model/model_mnist/train-images-idx3-ubyte.gz\n",
      "Extracting /home/lilin/model/model_mnist/train-labels-idx1-ubyte.gz\n",
      "Extracting /home/lilin/model/model_mnist/t10k-images-idx3-ubyte.gz\n",
      "Extracting /home/lilin/model/model_mnist/t10k-labels-idx1-ubyte.gz\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[0;31mSystemExit\u001b[0m\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/lilin/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py:2890: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.\n",
      "  warn(\"To exit: use 'exit', 'quit', or Ctrl-D.\", stacklevel=1)\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/env python2\n",
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "Created on Sun Nov  5 13:16:38 2017\n",
    "\n",
    "@author: lilin\n",
    "\"\"\"\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "SUMMARY_DIR=\"/home/lilin/model/model_mnist\"\n",
    "BATCH_SIZE=100\n",
    "TRAIN_STEPS=30000\n",
    "\n",
    "\n",
    "def variable_summaries(var,name):\n",
    "    with tf.name_scope('summaries'):\n",
    "        \n",
    "        tf.summary.histogram(name,var)\n",
    "        \n",
    "        \n",
    "        mean=tf.reduce_mean(var)\n",
    "        tf.summary.scalar('mean/'+name,mean)\n",
    "        \n",
    "        stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean)))\n",
    "        tf.summary.scalar('stddev/'+name,stddev)\n",
    "        \n",
    "        \n",
    "def nn_layer(input_tensor,input_dim,output_dim,layer_name,act=tf.nn.relu):\n",
    "    \n",
    "    with tf.name_scope(layer_name):\n",
    "        #generate the weights\n",
    "        with tf.name_scope('weights'):\n",
    "            weights=tf.Variable(tf.truncated_normal([input_dim,output_dim],stddev=0.1))\n",
    "            \n",
    "            variable_summaries(weights,layer_name+'/weights')\n",
    "            \n",
    "            \n",
    "        #generate the biase\n",
    "        with tf.name_scope('biases'):\n",
    "            biases=tf.Variable(tf.constant(0.0,shape=[output_dim]))\n",
    "            variable_summaries(biases,layer_name+'/biases')\n",
    "            \n",
    "        with tf.name_scope('Wx_plus_b'):\n",
    "            preactivate=tf.matmul(input_tensor,weights)+biases\n",
    "            \n",
    "            tf.summary.histogram(layer_name+'/pre_activations',preactivate)\n",
    "            \n",
    "        activations=act(preactivate,name='activation')\n",
    "        \n",
    "        tf.summary.histogram(layer_name+'/activations',activations)\n",
    "        \n",
    "        return activations\n",
    "    \n",
    "def main(_):\n",
    "    mnist=input_data.read_data_sets(\"/home/lilin/model/model_mnist\",one_hot=True)\n",
    "    \n",
    "    #define input\n",
    "    with tf.name_scope('input'):\n",
    "        x=tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "        y_=tf.placeholder(tf.float32,[None,10],name='y-input')\n",
    "        \n",
    "        #make the input into pixel matrix\n",
    "    with tf.name_scope('input_reshape'):\n",
    "        image_shaped_input=tf.reshape(x,[-1,28,28,1])\n",
    "        tf.summary.image('input',image_shaped_input,10)\n",
    "    \n",
    "    hidden1=nn_layer(x,784,500,'layer1')\n",
    "    y=nn_layer(hidden1,500,10,'layer2',act=tf.identity)\n",
    "    \n",
    "    \n",
    "    #compute cross_entropy\n",
    "    \n",
    "    with tf.name_scope('cross_entropy'):\n",
    "        cross_entropy=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y,labels=y_))\n",
    "        \n",
    "        tf.summary.scalar('cross_entropy',cross_entropy)\n",
    "        \n",
    "    with tf.name_scope('train'):\n",
    "        train_step=tf.train.AdamOptimizer(0.001).minimize(cross_entropy)\n",
    "        \n",
    "        \n",
    "    with tf.name_scope('accuracy'):\n",
    "        with tf.name_scope('correct_prediction'):\n",
    "            correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))\n",
    "            \n",
    "        with tf.name_scope('accuracy'):\n",
    "            accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "            \n",
    "        tf.summary.scalar('accuracy',accuracy)\n",
    "        \n",
    "    merged=tf.summary.merge_all()\n",
    "    \n",
    "    with tf.Session() as sess:\n",
    "        \n",
    "        summary_writer=tf.summary.FileWriter(SUMMARY_DIR,sess.graph)\n",
    "        tf.global_variables_initializer().run()\n",
    "        \n",
    "        for i in range(TRAIN_STEPS):\n",
    "            xs,ys=mnist.train.next_batch(BATCH_SIZE)\n",
    "            summary, _ =sess.run([merged,train_step],feed_dict={x:xs,y_:ys})\n",
    "            \n",
    "            summary_writer.add_summary(summary,i)\n",
    "            \n",
    "    summary_writer.close()\n",
    "    \n",
    "if __name__=='__main__':\n",
    "    tf.app.run()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
